<?php
class Controller_Admin_Related extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'related';
        parent::__construct($req, $res);

        $this->_list_where = array();

        $this->_sort_extra_columns = array();
    }

    public function action_index ()
    {
        $this->action_list();
    }

    public function action_list ($page = 1)
    {
        $this->_form = new Form_List($this->_form_type, $this->_list_pagesize, $this);
        $this->template->set('form_content', $this->_form->build_list($this->_list_where));
        $this->template->set_filename('smarty:admin/form/form');
    }

    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $packageName = Arr::get($post, 'packageName');
            $pnames      = Arr::get($post, 'pname', array());
            $serialize_packageNames = serialize($pnames);

            $post['serialize_packageNames'] = $serialize_packageNames;
            if (empty($packageName) || empty($pnames))
            {
                $this->request->redirect('/admin/related/create');
                return;
            }
                
            $this->_form_data = $post;
            parent::action_create();
        }
        else
        {
            parent::action_create();
        }

    }

    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $packageName = Arr::get($post, 'packageName');
            $pnames      = Arr::get($post, 'pname', array());
            $serialize_packageNames = serialize($pnames);

            $post['serialize_packageNames'] = $serialize_packageNames;
            if (empty($pnames))
            {
                $this->request->redirect('/admin/related/update/'.$nid);
                return;
            }

            unset($post['packageName']);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $pnames = unserialize($data['serialize_packageNames']);
            $arr_pnames = array();
            foreach ($pnames as $pname)
                $arr_pnames[] = array('pname' => $pname);
            $data['serialize_packageNames'] = $arr_pnames;

            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $pnames = unserialize($data['serialize_packageNames']);
        $arr_pnames = array();
        foreach ($pnames as $pname)
            $arr_pnames[] = array('pname' => $pname);
        $data['serialize_packageNames'] = $arr_pnames;
        $this->_form_data = $data;

        parent::action_detail($nid);
    }

    public function detail_filter_config_data (& $arr_config)
    {
        foreach ($arr_config as & $rs)
        {
            if ($rs['input_name'] == 'name')
            {
                $rs['class'] = 'line';
                $rs['input_type'] = 'text';
            }
        }
    }

    private function rebuild_post_data (& $post)
    {
        $pname = Arr::get($post, 'packageName');
        if (empty($pname))
            throw new Exception_Invalid("包名不能为空");

        $pinfo = API_Market::appinfo($pname);
        $post['name'] = Arr::get($pinfo, 'name');
        $post['cid']  = (int) Arr::get($pinfo, 'categoryId');
        $post['serialize_data'] = serialize($pinfo);
        $post['block_id'] = Arr::get($pinfo, 'block_id');
    }

    public function list_get_data ($offset, $limit)
    {
        $query = DB::select()
            ->from('app_related')
            ->execute();

        $apps = array();
        foreach ($query as $rs)
        {
            $pname = $rs['packageName'];
            $rapps = unserialize($rs['serialize_packageNames']);
            $apps[$pname] = array('id' => $rs['id'], 'packageName' => $pname, 'apps' => count($rapps), 'iconUrl' => '', 'name' => '');
        }

        $infos = API_Package::package_basic_infos(array_keys($apps));
        foreach ($apps as & $rs)
        {
            $pname = $rs['packageName'];
            if (empty($pname))
            {
                $rs['name'] = '默认';
                $rs['packageName'] = '--';
            }
            else if (array_key_exists($pname, $infos))
            {
                $rs['name'] = Arr::get($infos[$pname], 'name');
                $rs['iconUrl'] = Arr::get($infos[$pname], 'iconUrl');
            }
        }

        return $apps;
    }
}
